<!DOCTYPE HTML>
<html>
<head>
<script src="/js-test-resources/js-test.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>
description("Test whether WebSocket serialises connections correctly.");

window.jsTestIsAsync = true;

var receivedText = "";

var testIdPromise = new Promise(function(resolve, reject) {
    var ws = new WebSocket("ws://127.0.0.1:8880/connection-throttling?mode=new_test");
    ws.onmessage = function(event)
    {
        resolve(event.data);
    };
    ws.onclose = function(event)
    {
        reject();
    };
});

testIdPromise.then(function(testId) {
    return Promise.all([createTestPromise(testId), createTestPromise(testId)]);
}).then(finishJSTest, finishJSTest);

function createTestPromise(testId) {
    return new Promise(function(resolve, reject) {
        var ws = new WebSocket("ws://127.0.0.1:8880/connection-throttling?mode=do_test&id=" + testId);
        ws.onmessage = function(event)
        {
            receivedText = event.data;
            shouldBeEqualToString("receivedText", "PASS");
            resolve();
        };
        ws.onclose = function(event)
        {
            reject();
        };
    });
}
</script>
</body>
</html>
